﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FullThrustLogic.Core;
using FullThrustLogic.Core.GameState;
using FullThrustLogic.ShipComponent;
using FullThrustLogic.ShipComponent.Weapon;

namespace LogicTest.Controls
{
    public partial class FireControlAssignment : UserControl
    {
        public Ship SelectedShip;
        public  GameContext GameContext;
        public FireControlSystem SelectedFCS;

        public FireControlAssignment(Ship selectShip, FireControlSystem selectedFCS, GameContext gameContext)
        {
            SelectedShip = selectShip;
            GameContext = gameContext;
            SelectedFCS = selectedFCS;

            InitializeComponent();
        }

        private void cbShipsInRange_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadAvailableWeapons();
        }

        public void LoadAvailableWeapons()
        {
            lvWeapons.Clear();

            if (cbShipsInRange.SelectedItem is Ship)
            {
                bool inUse = false;

                foreach (iWeapon weapon in SelectedShip.Weapons)
                {
                    inUse = false;

                    foreach (FireControlSystem fcs in SelectedShip.FireControlSystems)
                    {
                        if (fcs != SelectedFCS)
                        {
                            iWeapon weapon1 = weapon;
                            if (fcs.AttackOrders.Find(obj => obj.TargetingWeapon == weapon1) != null)
                            {
                                inUse = true;
                            }
                        }
                    }

                    if (weapon.Enabled && !inUse)
                    {
                        if (((AttackOrderState)GameContext.CurrentState).IsShipInRange(SelectedShip, (Ship)cbShipsInRange.SelectedItem, weapon))
                        {
                            if (weapon is Beam)
                            {
                                ListViewItem item = new ListViewItem(string.Format("Beam, Class {0},", ((Beam)weapon).Class.ToString()));
                                item.Tag = weapon;
                                lvWeapons.Items.Add(item);
                            }
                            else if (weapon is Torpedo)
                            {
                                ListViewItem item = new ListViewItem("Torpedo");
                                item.Tag = weapon;
                                lvWeapons.Items.Add("Torpedo");
                            }
                        }
                    }
                }
            }
        }

        private void FireControlAssignment_Load(object sender, EventArgs e)
        {
            cbShipsInRange.Items.AddRange(SelectedShip.EnemyShipsInRange.ToArray());
            cbShipsInRange.SelectedIndex = 0;
        }
    }
}
